package com.azhoucode.controller;

import com.azhoucode.common.SimpleResponse;
import com.azhoucode.entity.ApplicationEntity;
import com.azhoucode.entity.TableEntity;
import com.azhoucode.service.DBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: db
 * @author: azhou
 * @create: 2021-11-01 14:50
 **/
@RestController
@RequestMapping("/db")
public class DBController {

    @Autowired
    private DBService dbService;

    /**
     * 校验数据库是否能够连接成功
     * @return
     */
    @PostMapping("/checkDBParam")
    public SimpleResponse<String> checkDBParam(@RequestBody ApplicationEntity applicationEntity) throws Exception {
        return SimpleResponse.success(dbService.checkDBParam(applicationEntity));
    }

    /**
     * 根据表名称获得表中的列数据
     * @param tableName
     * @return
     * @throws Exception
     */
    @GetMapping("/getTableColList/{tableName}/{random}")
    public SimpleResponse<TableEntity> getTableColList(@PathVariable("tableName") String tableName,@PathVariable("random") String random) throws Exception {
        return SimpleResponse.success(dbService.getTableColList(tableName,random));
    }

    /**
     * 获得所有的表数据
     * @param random
     * @return
     * @throws Exception
     */
    @GetMapping("/getAllTable/{random}")
    public SimpleResponse<List<String>> getAllTable(@PathVariable("random") String random) throws Exception {
        List<String> list = dbService.getAllTable(random);
        return SimpleResponse.success(list);
    }


}
